$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'sys/bbeginnerarticleinfo/queryList',
        datatype: "json",
        colModel: [			
			{ label: 'id', name: 'id', index: 'id',align: "center", width: 50, key: true },
			{ label: '公众号', name: 'gzhName', index: 'gzh_name',align: "center", width: 80 },
			{ label: '标签ID', name: 'articleTypeId', index: 'article_type_id',align: "center", width: 60, hidden: true },
			{ label: '标签名称', name: 'articleTypeName', index: 'articleTypeName',align: "center", width: 80 },
			{ label: '文章ID', name: 'articleId', index: 'article_id',align: "center", width: 60, formatter: function (value,options,rowData) {
					if (value == -1) {
						return "";
					} else {
						return value;
					}
				}
			},
			{ label: '文章名称', name: 'articleTitle', index: 'article_title',align: "center", width: 120 },
			{ label: '文章缩略图', name: 'articleImgUrl', index: 'article_img_url',align: "center", width: 60, formatter: function (value,options,rowData) {
					if (value) {
						return "<img src='"+value+"' height='60px' />";
					} else {
						return "";
					}
				}
			},
			{ label: '访问路径', name: 'accessUrl', index: 'access_url',align: "center", width: 120 },
			{ label: '状态', name: 'status', index: 'status',align: "center", width: 50, formatter: function (value,options,rowData) {
					if (value == 0) {
						return "<p style='color: red'>隐藏</p>"
					} else if (value == 1) {
						return "正常";
					}
				}
			},
			{ label: '排序', name: 'sort', index: 'sort',align: "center", width: 50 },
			{ label: '创建时间', name: 'createTime', index: 'create_time',align: "center", width: 90 },
			{ label: '修改时间', name: 'modifyTime', index: 'modify_time',align: "center", width: 90 },
        ],
		viewrecords: true,
        height: 520,
        rowNum: 15,
		rowList : [15,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
		postData:{"gzhCode":vm.q.gzhCode,"articleId":vm.q.articleId},
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });

	$.ajax({
		type: "POST",
		async:false,
		url: baseURL + 'sys/buserinfo/getUserGzhList',
		contentType: "application/json",
		success: function(r){
			if(r.code==0){
				vm.userGzhList = r.userGzhList;
			}
		}
	});
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		q:{
			gzhCode: '',
			articleId: null,
		},
		showList: true,
		title: null,
		bBeginnerArticleInfo: {},
		userGzhList: {},
		articleTypeIdList: {}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.bBeginnerArticleInfo = {};
			vm.bBeginnerArticleInfo.gzhCode = "";
			vm.bBeginnerArticleInfo.articleTypeId = "";
			vm.bBeginnerArticleInfo.status = 1;
			vm.bBeginnerArticleInfo.sort = 1;
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id);
		},
		saveOrUpdate: function (event) {
			if (!vm.bBeginnerArticleInfo.gzhCode || vm.bBeginnerArticleInfo.gzhCode == '') {
				alert("请选择公众号");
				return;
			}

			if (!vm.bBeginnerArticleInfo.articleTypeId || vm.bBeginnerArticleInfo.articleTypeId == '') {
				alert("请选择标签");
				return;
			}

			if (!vm.bBeginnerArticleInfo.sort || vm.bBeginnerArticleInfo.sort == '') {
				alert("请填写排序，只能为整数");
				return;
			}

			var url = vm.bBeginnerArticleInfo.id == null ? "sys/bbeginnerarticleinfo/save" : "sys/bbeginnerarticleinfo/update";
			$.ajax({
				type: "POST",
				url: baseURL + url,
				contentType: "application/json",
				data: JSON.stringify(vm.bBeginnerArticleInfo),
				success: function(r){
					if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		getInfo: function(id){
			$.get(baseURL + "sys/bbeginnerarticleinfo/info/"+id, function(r){
                vm.bBeginnerArticleInfo = r.bBeginnerArticleInfo;

                // 如果文章不是素材库文章，则不显示编号
                if (vm.bBeginnerArticleInfo.articleId == -1) {
					vm.bBeginnerArticleInfo.articleId = null;
				}
				vm.getArticleTypeIdByGzhCode(vm.bBeginnerArticleInfo.gzhCode, vm.bBeginnerArticleInfo.articleTypeId);
            });
		},
		/**
		 * 修改标签
		 * @param obj
		 */
		modifyArticleType: function (obj) {
			let gzhCode = $(obj).val();
			console.log("gzhCode = " + gzhCode);

			if (gzhCode == "") {
				vm.articleTypeIdList = {};
				vm.bBeginnerArticleInfo.articleTypeId = "";
			} else {
				vm.getArticleTypeIdByGzhCode(gzhCode, "");
			}
		},
		/**
		 * 根据公众号标识获取该公众号下的标签列表
		 * @param gzhCode 公众号标识
		 * @param articleTypeId 标签ID
		 */
		getArticleTypeIdByGzhCode: function (gzhCode, articleTypeId) {
			$.ajax({
				type: "GET",
				url: baseURL + "sys/bplatformbeginnerarticletype/getArticleTypeByGzhCode/" + gzhCode,
				success: function (r) {
					if (r.code === 0) {
						vm.articleTypeIdList = r.articleTypeIdList;
						vm.bBeginnerArticleInfo.articleTypeId = articleTypeId;
					} else {
						alert(r.msg);
					}
				}
			});
		},
		/**
		 * 校验文章是否存在或合法
		 * @param obj
		 */
		validateArticle: function (obj) {
			let articleId = $(obj).val();

			// 如果切换为空值，清除文章名称和图片信息
			if (!articleId || articleId.trim() == "") {
				$("#articleTitle").val("");
				$("#articleImgUrl").attr("src", "");
				vm.bBeginnerArticleInfo.articleTitle = "";
				vm.bBeginnerArticleInfo.articleImgUrl = "";
				return;
			}
			$.get(baseURL + "sys/barticleinfo/beginner/" + articleId, function (r) {
				if (r.code === 0) {

					let bArticleInfo = r.bArticleInfo;
					if (bArticleInfo) {
						$("#articleTitle").val(bArticleInfo.articleTitle);
						$("#articleImgUrl").attr("src", bArticleInfo.articleImgUrl);
						vm.bBeginnerArticleInfo.articleTitle = bArticleInfo.articleTitle;
						vm.bBeginnerArticleInfo.articleImgUrl = bArticleInfo.articleImgUrl;
					} else {
						$("#articleTitle").val("");
						$("#articleImgUrl").attr("src", "");
						vm.bBeginnerArticleInfo.articleTitle = "";
						vm.bBeginnerArticleInfo.articleImgUrl = "";
					}

					console.log("[articleTitle] = " + vm.bBeginnerArticleInfo.articleTitle);
					console.log("[articleImgUrl] = " + vm.bBeginnerArticleInfo.articleImgUrl);

					let content = "";
					for (let item in vm.bBeginnerArticleInfo) {
						content += item + " : " + vm.bBeginnerArticleInfo[item] + "\n";
					}
					console.log("[bBeginnerArticleInfo] = " + content);
				}
			});
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{
				postData:{"gzhCode":vm.q.gzhCode,"articleId":vm.q.articleId},
                page:page
            }).trigger("reloadGrid");
		}
	}
});